package com.lvmama.controller.system;

import com.lvmama.common.exception.BizException;
import com.lvmama.common.result.Result;
import com.lvmama.pojo.base.Page;
import com.lvmama.pojo.base.ResultBase;
import com.lvmama.pojo.dto.rhino.system.UserDTO;
import com.lvmama.pojo.dto.rhino.system.UserQueryDTO;
import com.lvmama.pojo.entity.rhino.system.User;
import com.lvmama.service.system.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findByUsername")
    public ResultBase<User> findByUsername(String username) {

        ResultBase<User> result;
        try {
            result = userService.queryUserAndRolesByUsername(username);
        }catch (BizException e){
            log.error("loadUserByUsername error:{}", e.getMessage(), e);
            return null;
        }
        return result;
    }

    @PostMapping("/getUserPage")
    public Result<Page<User>> getUserPage(@RequestBody UserQueryDTO userQueryDTO){
        return userService.queryUserPage(userQueryDTO);
    }
    @PostMapping("deleteUserById")
    public Result<Boolean> deleteUserById(@RequestParam("id") String id){
        return userService.deleteUserById(id);
    }
    @PostMapping("saveUserByUserDTO")
    Result saveUserByUserDTO(@RequestBody UserDTO userDTO){
        return userService.saveUserByUserDTO(userDTO);
    }
    @PostMapping("updateUserByUserDTO")
    Result updateUserByUserDTO(@RequestBody UserDTO userDTO){
        return userService.updateUserByUserDTO(userDTO);
    }

    @PostMapping("getUserList")
    Result<List<User>> getUserList(@RequestBody  UserQueryDTO queryDTO){
        return userService.queryUserList(queryDTO);
    }

    @PostMapping("getUserRoleById")
    Result<User> getUserRoleById(@RequestParam("id") String id){
        return userService.queryUserRoleById(id);
    }
    @PostMapping("enableUserById")
    Result<Integer> enableUserById(@RequestParam("id") String id){
        return userService.enableUserById(id);
    }
    @PostMapping("updateUserLoginDateByUserId")
    Result updateUserLoginDateByUserId(@RequestParam("id") String id){
        return userService.updateUserOperateDateByUserId(id);
    }
}
